<?php
var_dump(gettype([]));
var_dump(gettype(1));
var_dump(gettype('sss'));
var_dump(gettype('111'));




exit;
ini_set('memory_limit', '1024M');

$content = file_get_contents('/Users/amumu/Downloads/nginx.txt');
$content = explode("\n", $content);
echo count($content) . PHP_EOL;
//echo $content[1] . PHP_EOL;
$ips = [];
$apis = [];
foreach ($content as $value) {
    $value = explode(' ', $value);

    //统计IP
    $ip = str_replace('./access.log:', '', $value[0]);
    if (!in_array($ip, $ips)) {
        $ips[] = $ip;
    }
    //统计每个IP接口
    $api = $value[6];
    if (strpos($api, 'task/list/status')) {
        continue;
    }
    if (strpos($api, 'task/add/gift/record')) {
        continue;
    }

    $api = explode('?', $api);
    $api = $api[0];

    if (!strpos($api, 'has/new/message')) {
        continue;
    }
    $time = str_replace('[25/Jul/', '', $value[3]);

    $apis[$api][$ip][$time]++;

//    if(!in_array($api,$apis)){
//        $apis[]=$api;
//    }

    //echo $api . PHP_EOL;
    //exit;

//
//    var_dump($value);
//    exit;
    //echo $value . PHP_EOL;
}

foreach ($apis as $api => $ips) {
    foreach ($ips as $ip => $count) {
        echo $api . '----' . $count . '----' . $ip . PHP_EOL;
    }
}
//var_dump($apis);
//echo count($apis) . PHP_EOL;
exit;

function setRandomGroupRule($robots)
{
    //随机组数
    $group_num = random_int(3, 5);
    $group_array = [];
    $result = [];
    for ($i = 0; $i < $group_num; $i++) {
        $group_array[] = $i;
        $result[$i] = [];
    }
    shuffle($group_array);
    //机器人总数
    $count = count($robots);

    //随机分配
    foreach ($group_array as $m => $group) {
        //若是最后一组且机器人还有剩余,则把剩余的全部赋予最后一组
        if ($m === $group_num - 1) {
            $result[$group] = array_values($robots);
            break;
        }
        //生成一个组数
        $n = mt_rand(0, $count);
        if ($n === 0) {
            continue;
        }
        $keys = array_rand($robots, $n);
        if ($n === 1) {
            $result[$group][] = $robots[$keys];
            unset($robots[$keys]);
        } else {
            foreach ($keys as $key) {
                $result[$group][] = $robots[$key];
                unset($robots[$key]);
            }
        }
        $count = count($robots);
        if ($count === 0) {
            break;
        }
    }

    return $result;
}


$array = ['a' => 'ssss', 0 => 'aaaaa', 'dddd', 'eeeee', 'rrrr', 'tttt', '66666', 'yyyyy', 'uuuuu', 'ooooo', 'ffffff', 'ggggg', 'hhhh', 'jjjjjj', 'kkkk', 'lllll'];

var_dump(count($array));

$re = setRandomGroupRule($array);
$count = 0;
foreach ($re as $v) {
    $count += count($v);
}


var_dump($count);

exit;
$n = array_rand($a);
unset($a[$n]);
var_dump($a);
$n = array_rand($a);
unset($a[$n]);
var_dump($a);
$n = array_rand($a);
unset($a[$n]);
var_dump($a);
$n = array_rand($a);
unset($a[$n]);
var_dump($a);

exit;

$html = '<body>123</body>';

$result = htmlspecialchars_decode($html);

var_dump($result);

exit;

$array1 = array(
    array('id' => 8, 'name' => 'Apple', 'age' => 18),
    array('id' => 7, 'name' => 'Bed', 'age' => 17),
    array('id' => 5, 'name' => 'Cos', 'age' => 16),
    array('id' => 5, 'name' => 'Cos', 'age' => 14),
    array('id' => 5, 'name' => 'Cos', 'age' => 13),
    array('id' => 5, 'name' => 'Cos', 'age' => 12),
    array('id' => 5, 'name' => 'Cos', 'age' => 11),
    array('id' => 5, 'name' => 'Cos', 'age' => 10),
    array('id' => 5, 'name' => 'Cos', 'age' => 10),
);
function sortArrByManyField()
{
    $args = func_get_args();
    if (empty($args)) {
        return null;
    }
    $arr = array_shift($args);
    if (!is_array($arr)) {
        throw new Exception("第一个参数不为数组");
    }
    foreach ($args as $key => $field) {
        if (is_string($field)) {
            $temp = array();
            foreach ($arr as $index => $val) {
                $temp[$index] = $val[$field];
            }
            $args[$key] = $temp;
        }
    }
    $args[] = &$arr;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}

$arr = sortArrByManyField($array1, 'id', SORT_ASC, 'name', SORT_ASC, 'age', SORT_DESC);
print_r($arr);

exit;


class WordMatcher
{

    public static function match($word, $str)
    {
        if (empty($str)) {
            return;
        }
        $len = mb_strlen($word, 'utf-8');
        $str_len = mb_strlen($str, 'utf-8');

        $matched = [];
        for ($i = 0; $i < $str_len; $i++) {
            $substr = mb_substr($str, $i, $len, 'utf-8');
            if ($substr === $word) {
                $matched[] = $substr;
            }
        }
        return !empty($matched);
    }
}


$matched = WordMatcher::match('最好的', 'PHP是最好的语言');
//WordMatcher::match('语言s', 'PHP是最好的语言');

var_dump($matched);

exit;


$server = new Swoole\WebSocket\Server("0.0.0.0", 9502);

$server->on('open', function (Swoole\WebSocket\Server $server, $request) {
    echo "server: handshake success with fd{$request->fd}\n";
});

$server->on('message', function (Swoole\WebSocket\Server $server, $frame) {
    echo "receive from {$frame->fd}:{$frame->data},opcode:{$frame->opcode},fin:{$frame->finish}\n";
    $server->push($frame->fd, "this is server");
});

$server->on('close', function ($ser, $fd) {
    echo "client {$fd} closed\n";
});

$server->start();